﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.IN;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using System.Windows.Forms;

namespace HiStaff.iInsurance
{
    public partial class RegimeInsurance : FormBase
    {
        public RegimeInsurance()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<IN_ENTITLED>("APRS_IN_ENTITLED", 
                    new SEARCH { TERMINATE = chkTerminate.Value,
                        ORGID = trvOrg.OrgId,
                        CHGFROM = hiDatePicker2.Value, 
                        CHGTO = hiDatePicker1.Value });

                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        protected override bool SaveButtonClick()
        {
            try
            {
                var objSave = new IN_ENTITLED();
                ValueControlsToObject(objSave, this);
                if (chkThanhToan .Checked == true)
                {
                    objSave.PAYMENT = 0;
                }
                else
                {
                    objSave.PAYMENT = 1;
                }
                
                if (BaseStatus == Util.Enum.FormStatus.Add)
                {
                    objSave.ID = 0;
                }
                OracleHelper.ExcuteNonQuery("APRU_IN_ENTITLED", objSave);
                BindData();
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        protected override bool CancelButtonClick()
        {
            try
            {
                BindData();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        //chưa sửa store
        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_IN_ENTITLED", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }
        private void BindDataFromGrid()
        {
            BindDataFromGridToForm(grdMain, this);
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGrid();
            lblInsName.Text = hiEmployeeSelection2.SelectedInsCenterName;
        }

        private void hiEmployeeSelection2_SelectedEmployeeChanged(object sender, EventArgs e)
        {
            lblInsName.Text = hiEmployeeSelection2.SelectedInsCenterName;
            hiSalarySelection1.EmployeeID = hiEmployeeSelection2.SelectedID;

        }

    
        private void hiDatePicker4_Validated(object sender, EventArgs e)
        {
            if (!Common.CheckEffectDateAndExpireDate(dtpDateFrom, dtpDateTo))
            {
                dtpDateTo.Focus();
                MessageBox.Show("Thông tin nhập từ ngày đến ngày không hợp lệ");
                txtCoutOff.Text = String.Empty;
            }
            else if (dtpDateFrom.Value == dtpDateTo.Value)
            { txtCoutOff.Text = "1"; }
            else
            {
                String s = (dtpDateTo.Value - dtpDateFrom.Value).ToString();
                txtCoutOff.Text = (String.Format("{0}{1}", int.Parse(s.Substring(0, s.IndexOf('.'))), 1)).ToString();
            }
        }
    }
}
